2669. Rotation

 

The array of size n × m is given. Rotate it 90 degrees clockwise.

 

Input. The first line contains two integers n and m (1 ≤ n, m ≤ 50). Each of the next n lines contains m nonnegative integers, not greater than 109 – it is the array itself.

 

Output. Print the rotated array in the same format as input.

 

Sample input

Sample output

3 4
1 2 3 4
5 6 7 8

9 10 11 12

4 3

9 5 1

10 6 2

11 7 3

12 8 4

 

 

SOLUTION

two-dinensional array

 

Algorithm analysis

When rotated 90 degrees clockwise, the element of the matrix a with coordinates (i, j) moves to the element of the matrix b with coordinates (j, ni – 1). If the size of the matrix a is n × m, then the size of the matrix b will be m × n. Both matrices are indexed from 0.

Algorithm realization

Declare the input array a and the resulting rotated array b.

 

#define MAX 55

int a[MAX][MAX], b[MAX][MAX];

 

Read the input array.

 

scanf("%d %d",&n,&m);

for(i = 0; i < n; i++)

for(j = 0; j < m; j++)

  scanf("%d",&a[i][j]);

 

Rotate array а into b.

 

for(i = 0; i < n; i++)

for(j = 0; j < m; j++)

  b[j][n-i-1] = a[i][j];

 

Print the rotated array b.

 

printf("%d %d\n",m,n);

for(i = 0; i < m; i++)

{

  for(j = 0; j < n; j++)

    printf("%d ",b[i][j]);

  printf("\n");

}